interface ClientType {
  name: string;
}

interface Client {
  name: string;
  phoneNumber: string;
  sampling: boolean;
  type: ClientType;
  createtime: string
  content: string;
}

interface Item {
  status: 'wait' | 'complete' | 'overtime'; // 假设这些是你所有可能的状态  
  statusName: string,
  orderNumber: string,
  client: Client;
  id?: string | number,
}

Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: '/pages/toDoList/toDoList'
        })
      }
      this.onLoad();
    }
  },
  properties: {
    type: {
      type: Number,
      value: 1,
    },
    isMy: {
      type: Boolean,
      value: false,
    }
  },
  data: {
    triggered: false,
    loading: false,
    list: [] as Array<Item>,
    page: {
      pageSize: 10,
      currentPage: 1,
      next: true,
    },
    show: false,
  },
  observers: {
    type(val) {
      this.setData({
        list: []
      }, () => {
        this.refresh();
      }) 
    }
  },
  methods: {
  
    onClose() {
      this.setData({ show: false });
    },
    onLoad() {
      return new Promise<void>(resole => {
        const item: Item = {
          status: 'wait',
          statusName: '待处理',
          orderNumber: '12345678',
          client: {
            name: '嘻嘻嘻',
            phoneNumber: '13793275211',
            sampling: true,
            type: {
              name: '全屋整理',
            },
            content: '全屋整理',
            createtime: '2024/05/15 16:17:00'
          },
        };
        const list: Array<Item> = [];
        const index = (this.data.page.currentPage-1) * this.data.page.pageSize
        for (let i = index+1; i <= index + 10; i++) {
          list.push({ ...item, id: i });
        }
        this.setData({
          loading: true
        })
        setTimeout(() => {
          if(list.length < this.data.page.pageSize) {
            this.setData({
              'page.next': false
            })
          }
          if (this.data.page.currentPage === 1) {
            this.setData({
              list: list,
              loading: false
            })
          } else {
            this.setData({
              list: [...this.data.list, ...list],
              loading: false
            })
          }
          resole();
        },1000)
      })
    },
    refresh() {
      this.setData({
        'page.currentPage': 1
      },async () => {
        await this.onLoad();
        this.setData({
          triggered: false
        })
      });
    },
    next(){
      if(!this.data.page.next) return;
      this.setData({
        'page.currentPage': this.data.page.currentPage+1
      }, () => {
        this.onLoad();
      })
    }
  }
})
